package com.simple.util;

import java.util.HashMap;
import java.util.Map;

public class CompareTextUtil {
	
	public static float compare(String text1, String text2) {
		String[] texts1 = text1.split(" ");
		String[] texts2 = text2.split(" ");
		if(texts1.length < 2 || texts2.length < 2) {
			return 0;
		}
		int score1 = 0;
		int score2 = 0;
		Map<String,Integer> map2 = new HashMap<>();
		int getScore = 0;
		
		for(String key: texts1) {
			if(!key.equals("")) {
				score1 += key.length();
			}
		}
		
		for(String key: texts2) {
			if(!key.equals("")) {
				Integer count = map2.get(key);
				if(count == null) {
					count = 0;
				}
				count++;
				map2.put(key, count);
				score2 += key.length();
			}
		}
		
		int totol = score1 + score2;
		
		for(String key: texts1) {
			if(map2.containsKey(key)) {
				getScore += key.length();
				totol -= key.length();
				int count = map2.get(key);
				count--;
				if(count == 0) {
					map2.remove(key);
				}
				else {
					map2.put(key, count);
				}
			}
		}
		
		return ((float)getScore) / totol;
	}
	
	public static float compare(char[] texts1, char[] texts2) {
		if(texts1.length < 2 || texts2.length < 2) {
			return 0;
		}
		int score1 = 0;
		int score2 = 0;
		Map<Character,Integer> map2 = new HashMap<>();
		int getScore = 0;
		
		score1 = texts1.length;
		
		for(char key: texts2) {
			Integer count = map2.get(key);
			if(count == null) {
				count = 0;
			}
			count++;
			map2.put(key, count);
			score2 += 1;
		}
		
		int totol = score1 + score2;
		
		for(char key: texts1) {
			if(map2.containsKey(key)) {
				getScore += 1;
				totol -= 1;
				int count = map2.get(key);
				count--;
				if(count == 0) {
					map2.remove(key);
				}
				else {
					map2.put(key, count);
				}
			}
		}
		
		return ((float)getScore) / totol;
	}
	
	public static void main(String[] args) {
		
		System.out.println(compare("化 简 x x 1 x 1 x 2", "化 简 x 1 x 2"));
		System.out.println(compare("化简xx1x1x2".toCharArray(), "化简x1x2".toCharArray()));
	}
}
